﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

using RexToy.UI.WinForm;
using RexToy.UI.WinForm.ViewModel;
using RexToy.UI.WinForm.Controls;

using Studio.ViewModel;

namespace Studio.UI
{
    public class FrmMain : MDIContainerForm, IEventSubscriber
    {
        public FrmMain()
        {
            var vm = this.ViewModel as FrmMainVM;
            vm.AboutCommand = new ActionCommand(this.AboutAction);
            vm.DesignerCommand = new ActionCommand(this.OpenDesignerAction);
            vm.ConnectCommand = new ActionCommand(this.ConnectAction, vm.OnConnectCanExecute);

            vm.PropertyChanged += new PropertyChangedEventHandler(ViewModel_PropertyChanged);

            EventBroadcaster.Instance().Subscribe(Constants.Broadcast_Template_Err, this);
        }

        protected override void OnLoad(EventArgs e)
        {
            CommandBindRegistry.Instance().RequeryCanExecute();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (System.Windows.Forms.MessageBox.Show(this, "Quit?", "Message", System.Windows.Forms.MessageBoxButtons.OKCancel) != System.Windows.Forms.DialogResult.OK)
                e.Cancel = true;
        }

        private void AboutAction()
        {
            var frm = AppVM.ViewProvider.Lookup<RexToy.UI.WinForm.Controls.Form>(Constants.Form_About);
            frm.ShowDialog();
        }

        private void OpenDesignerAction()
        {
            var frm = AppVM.ViewProvider.Lookup<RexToy.UI.WinForm.Controls.Form>(Constants.Form_TemplateDesigner);
            frm.MdiParent = AppVM.ViewProvider.Lookup<RexToy.UI.WinForm.Controls.Form>(Constants.Form_Main);
            var vmMain = this.ViewModel as FrmMainVM;
            var vmDesigner = frm.ViewModel as FrmTemplateDesignerVM;
            vmMain.AppendDesigner(vmDesigner);
            frm.Show();
        }

        private void ConnectAction()
        {
            var frm = AppVM.ViewProvider.Lookup<RexToy.UI.WinForm.Controls.Form>(Constants.Form_Connect);
            var cnnVM = frm.ViewModel as FrmConnectVM;
            cnnVM.SetMainVM(this.ViewModel as FrmMainVM);
            frm.ShowDialog();
        }

        void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertyExtract.ExtractName<FrmMainVM>(vm => vm.ConnectTestResult))
            {
                var vm = sender as FrmMainVM;
                System.Windows.Forms.MessageBox.Show(vm.ConnectTestResult);
            }
        }

        public void EventNotify(object sender, BroadcastEventArgs args)
        {
            var frm = AppVM.ViewProvider.Lookup<RexToy.UI.WinForm.Controls.Form>(Constants.Form_Exception);
            var vm = frm.ViewModel as FrmExceptionVM;
            vm.Error = args.Payload as Exception;
            frm.ShowDialog(this);
        }
    }
}